<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户统计 - 管理员后台</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        /* 保持原有样式不变 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
        }

        :root {
            --primary: #2c3e50;
            --secondary: #34495e;
            --accent: #e74c3c;
            --light: #ecf0f1;
            --dark: #2c3e50;
            --success: #2ecc71;
            --warning: #f39c12;
            --info: #3498db;
            --gray: #95a5a6;
            --light-gray: #f5f7fa;
            --border: #e0e6ed;
            --card-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            --hover-shadow: 0 6px 12px rgba(0, 0, 0, 0.1);
        }

        body {
            background-color: var(--light-gray);
            color: #333;
            overflow-x: hidden;
        }

        .admin-container {
            display: flex;
            min-height: 100vh;
        }

        /* 侧边栏样式 */
        .admin-sidebar {
            width: 250px;
            background: var(--primary);
            color: white;
            padding: 20px 0;
            transition: all 0.3s ease;
            position: fixed;
            height: 100vh;
            overflow-y: auto;
            z-index: 1000;
        }

        .admin-logo {
            text-align: center;
            padding: 20px 0;
            border-bottom: 1px solid var(--secondary);
            margin-bottom: 20px;
        }

        .admin-logo h2 {
            font-size: 1.5rem;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 10px;
        }

        .admin-menu {
            list-style: none;
            padding: 0;
        }

        .admin-menu li a {
            display: flex;
            align-items: center;
            padding: 15px 30px;
            color: var(--light);
            text-decoration: none;
            transition: all 0.3s;
            border-left: 4px solid transparent;
            font-size: 1rem;
        }

        .admin-menu li a:hover,
        .admin-menu li a.active {
            background: var(--secondary);
            border-left: 4px solid var(--accent);
        }

        .admin-menu li a i {
            width: 25px;
            text-align: center;
            margin-right: 12px;
            font-size: 1.1rem;
        }

        /* 主内容区样式 */
        .admin-content {
            flex: 1;
            padding: 30px;
            margin-left: 250px;
            transition: margin-left 0.3s;
        }

        .admin-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 20px;
            border-bottom: 1px solid var(--border);
        }

        .admin-header h1 {
            font-size: 1.8rem;
            color: var(--dark);
        }

        .admin-header h1 span {
            color: var(--accent);
        }

        .admin-header .date-info {
            font-size: 1.1rem;
            color: var(--gray);
            background: var(--light);
            padding: 8px 15px;
            border-radius: 30px;
            box-shadow: var(--card-shadow);
        }

        /* 用户管理特定样式 */
        .user-management-container {
            background: white;
            border-radius: 12px;
            padding: 25px;
            box-shadow: var(--card-shadow);
        }

        .user-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 25px;
        }

        .user-header h2 {
            color: var(--dark);
            font-size: 1.5rem;
        }

        .user-actions {
            display: flex;
            gap: 15px;
        }

        .user-actions button {
            background: var(--accent);
            color: white;
            border: none;
            padding: 8px 15px;
            border-radius: 20px;
            cursor: pointer;
            transition: all 0.3s;
            font-size: 0.9rem;
            display: flex;
            align-items: center;
            gap: 5px;
        }

        .user-actions button:hover {
            background: var(--secondary);
        }

        .user-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }

        .user-table th, .user-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid var(--border);
        }

        .user-table th {
            background-color: var(--light-gray);
            font-weight: 600;
            color: var(--dark);
        }

        .user-table tr:hover {
            background-color: var(--light-gray);
        }

        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            object-fit: cover;
        }

        .user-status {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 0.85rem;
            font-weight: 500;
        }

        .status-active {
            background-color: rgba(46, 204, 113, 0.1);
            color: var(--success);
        }

        .status-inactive {
            background-color: rgba(231, 76, 60, 0.1);
            color: var(--accent);
        }

        .user-actions-cell {
            height: 100px;
            gap: 10px;
        }

        .action-btn {
            padding: 5px 10px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 0.85rem;
            border: none;
            transition: all 0.2s;
        }

        .view-btn {
            background-color: var(--success);
            color: white;
        }

        .view-btn:hover {
            background-color: #27ae60;
        }

        .search-bar {
            display: flex;
            margin-bottom: 20px;
            gap: 10px;
        }

        .search-bar input {
            flex: 1;
            padding: 10px 15px;
            border: 1px solid var(--border);
            border-radius: 4px;
            font-size: 1rem;
        }

        .search-bar button {
            background: var(--accent);
            color: white;
            border: none;
            padding: 0 20px;
            border-radius: 4px;
            cursor: pointer;
        }

        .pagination {
            display: flex;
            justify-content: center;
            margin-top: 20px;
            gap: 5px;
        }

        .pagination button {
            padding: 8px 12px;
            border: 1px solid var(--border);
            background: white;
            cursor: pointer;
            border-radius: 4px;
        }

        .pagination button.active {
            background: var(--accent);
            color: white;
            border-color: var(--accent);
        }

        .pagination button:hover:not(.active) {
            background: var(--light-gray);
        }

        .pagination button:disabled {
            opacity: 0.5;
            cursor: not-allowed;
        }
    </style>
</head>
<body>
<div class="admin-container">
    <!-- 侧边栏 -->
    <div class="admin-sidebar">
        <div class="admin-logo">
            <h2><i class="fas fa-user-cog"></i> 管理员后台</h2>
        </div>
        <ul class="admin-menu">
            <li><a href="${pageContext.request.contextPath}/dashboard"
                   class="${activeTab == 'dashboard' ? 'active' : ''}"><i class="fas fa-chart-bar"></i> 数据统计</a></li>
            <li><a href="${pageContext.request.contextPath}/userManage"
                   class="${activeTab == 'user' ? 'active' : ''}"><i class="fas fa-users"></i> 用户统计</a></li>
            <li><a href="${pageContext.request.contextPath}/productManage"
                   class="${activeTab == 'product' ? 'active' : ''}"><i class="fas fa-box"></i> 商品统计</a></li>
            <li><a href="${pageContext.request.contextPath}/orderManege"
                   class="${activeTab == 'order' ? 'active' : ''}"><i class="fas fa-shopping-cart"></i> 订单统计</a></li>
            <li><a href="${pageContext.request.contextPath}/logout" id="logoutLink">
                <i class="fas fa-sign-out-alt"></i> 退出登录
            </a></li>
        </ul>
    </div>

    <!-- 主内容区 -->
    <div class="admin-content">
        <div class="admin-header">
            <h1>用户统计</h1>
            <div class="date-info">
                <i class="fas fa-calendar-alt"></i> <span id="current-date"></span>
            </div>
        </div>

        <div class="user-management-container">
            <div class="user-header">
                <h2>用户列表</h2>
                <div class="user-actions">
                    <button id="exportBtn">
                        <i class="fas fa-file-export"></i> 导出数据
                    </button>
                </div>
            </div>

            <form id="searchForm" action="${pageContext.request.contextPath}/userManage" method="get">
                <div class="search-bar">
                    <input type="text" name="keyword" placeholder="搜索用户名、学号或手机号..." value="${param.keyword}">
                    <button type="submit"><i class="fas fa-search"></i></button>
                </div>
            </form>

            <table class="user-table">
                <thead>
                <tr>
                    <th>ID</th>
                    <th>头像</th>
                    <th>用户名</th>
                    <th>真实姓名</th>
                    <th>学号</th>
                    <th>邮箱</th>
                    <th>手机</th>
                    <th>状态</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${users}" var="user">
                    <c:set var="now" value="<%=new java.util.Date()%>"/>
                    <c:set var="threeDaysAgo"
                           value="<%=new java.util.Date(new java.util.Date().getTime() - 3*24*60*60*1000)%>"/>
                    <c:set var="isActive" value="${user.lastLoginTime.time > threeDaysAgo.time}"/>

                    <tr>
                        <td>${user.userId}</td>
                        <td>
                            <img src="${pageContext.request.contextPath}/Market/${user.avatar}"
                                 alt="头像"
                                 onerror="this.src='${pageContext.request.contextPath}/Market/default-avatar.jpg'"
                                 class="user-avatar">
                        </td>
                        <td>${user.username}</td>
                        <td>${user.realName}</td>
                        <td>${user.studentId}</td>
                        <td>${user.email}</td>
                        <td>${user.phone}</td>
                        <td>
                            <span class="user-status ${isActive ? 'status-active' : 'status-inactive'}">
                                <i class="fas ${isActive ? 'fa-check-circle' : 'fa-clock'}"></i>
                                ${isActive ? '活跃' : '不活跃'}
                            </span>
                        </td>
                        <td class="user-actions-cell">
                            <button class="action-btn view-btn" onclick="viewUser(${user.userId})">
                                <i class="fas fa-eye"></i> 查看
                            </button>
                        </td>
                    </tr>
                </c:forEach>
                <c:if test="${empty users}">
                    <tr>
                        <td colspan="10" style="text-align: center;">没有找到符合条件的用户</td>
                    </tr>
                </c:if>
                </tbody>
            </table>

            <div class="pagination">
                <%-- 上一页按钮 --%>
                <c:if test="${currentPage > 1}">
                    <button onclick="goToPage(1)"><i class="fas fa-angle-double-left"></i></button>
                    <button onclick="goToPage(${currentPage - 1})"><i class="fas fa-angle-left"></i></button>
                </c:if>
                <c:if test="${currentPage <= 1}">
                    <button disabled><i class="fas fa-angle-double-left"></i></button>
                    <button disabled><i class="fas fa-angle-left"></i></button>
                </c:if>

                <%-- 页码按钮 --%>
                <c:forEach begin="1" end="${totalPages}" var="pageNum">
                    <button class="${currentPage == pageNum ? 'active' : ''}"
                            onclick="goToPage(${pageNum})">${pageNum}</button>
                </c:forEach>

                <%-- 下一页按钮 --%>
                <c:if test="${currentPage < totalPages}">
                    <button onclick="goToPage(${currentPage + 1})"><i class="fas fa-angle-right"></i></button>
                    <button onclick="goToPage(${totalPages})"><i class="fas fa-angle-double-right"></i></button>
                </c:if>
                <c:if test="${currentPage >= totalPages}">
                    <button disabled><i class="fas fa-angle-right"></i></button>
                    <button disabled><i class="fas fa-angle-double-right"></i></button>
                </c:if>
            </div>
        </div>
    </div>
</div>

<script>
    // 显示当前日期和星期
    function updateCurrentTime() {
        const now = new Date();

        // 配置日期和星期显示格式
        const options = {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            weekday: 'long',  // 显示完整的星期（如"星期一"）
            hour: '2-digit',
            minute: '2-digit',
            second: '2-digit',
            hour12: false      // 24小时制
        };

        // 转换为本地化字符串（中文）
        const dateTimeStr = now.toLocaleString('zh-CN', options);

        // 更新到页面
        document.getElementById('current-date').textContent = dateTimeStr;
    }

    // 初始化并每秒更新
    updateCurrentTime();
    setInterval(updateCurrentTime, 1000);

    function viewUser(userId) {
        window.location.href = '${pageContext.request.contextPath}/detail?id=' + userId;
    }

    function goToPage(pageNum) {
        const keyword = '${keyword}';
        let url = '${pageContext.request.contextPath}/userManage?page=' + pageNum;
        if (keyword) {
            url += '&keyword=' + encodeURIComponent(keyword);
        }
        window.location.href = url;
    }

    // 导出数据按钮事件
    document.getElementById('exportBtn').addEventListener('click', function () {
        const keyword = '${param.keyword}';
        const pageSize = '${pageSize}';
        const currentPage = '${currentPage}';

        let url = '${pageContext.request.contextPath}/user/export';
        let params = [];

        if (keyword) params.push('keyword=' + encodeURIComponent(keyword));
        if (pageSize && pageSize !== '10') params.push('pageSize=' + pageSize);
        if (currentPage && currentPage !== '1') params.push('page=' + currentPage);

        if (params.length > 0) {
            url += '?' + params.join('&');
        }

        // 创建一个隐藏的iframe来触发下载
        const iframe = document.createElement('iframe');
        iframe.style.display = 'none';
        iframe.src = url;
        document.body.appendChild(iframe);

        // 设置3秒后移除iframe，避免DOM堆积
        setTimeout(() => {
            document.body.removeChild(iframe);
        }, 3000);
    });
</script>
</body>
</html>